package com.zxl.study.config;

import com.github.benmanes.caffeine.cache.Cache;
import io.lettuce.core.support.caching.CacheAccessor;
import lombok.extern.slf4j.Slf4j;

/**
 * @auther zxl
 * @create 2024-01-24 18:32
 */
@Slf4j
public class CaffeineCacheAccessor implements CacheAccessor {
    private Cache localCacheCaffeine;

    public CaffeineCacheAccessor(Cache cache) {
        this.localCacheCaffeine = cache;
    }

    /**
     * 读取缓存
     */
    @Override
    public Object get(Object key) {
        log.info("caffeine get:{}", key);
        return localCacheCaffeine.getIfPresent(key);
    }

    /**
     * 写入缓存
     */
    @Override
    public void put(Object key, Object value) {
        log.info("caffeine put:{},{}", key, value);
        localCacheCaffeine.put(key, value);
    }

    /**
     * 清空缓存
     */
    @Override
    public void evict(Object key) {
        log.info("caffeine evict:{}", key);
        localCacheCaffeine.invalidate(key);
    }
}
